﻿using System;
using System.Data;

namespace Sqlite
{
   public  static  class InsertLog
    {
        private static  string  SqliteName { get; set; }

        private static  bool initRight=false  ;

        public static bool InitRight
        {
            get { return initRight ; }
            set { initRight  = value; }
        }


        public static void  LogInit (string DBName)
        {
            SqliteName = DBName;
            sqlhelper= new SQLiteDBHelper(SqliteName);
        }
        private  static  SQLiteDBHelper sqlhelper;
        public   static  void CreatDB()
        {
            SQLiteDBHelper.CreateDB(SqliteName);
        }




        /// <summary>
        /// 对表SockLib写入SockLib库工程异常信息
        /// </summary>
        /// <param name="infos">
        /// <para>数组个数必须等于3</para>
        /// <para>infos[0]:端口号</para>
        /// <para>infos[1]:错误信息</para>
        /// <para>infos[2]:堆栈信息</para>
        /// </param>
        /// <returns>返回插入的条数，-1代表传入参数错误</returns>
        public static  int  InsertSockLib(params object[] infos)
        {
                if (infos.Length != 3 || InitRight == false)
                    return -1;
                string cmd = "insert into SockLib(Port,DateTime,Msg,Stack) values(@port,@datetime,@msg,@stack)";
                return sqlhelper.ExecuteNonQuery(cmd,
                     new System.Data.SQLite.SQLiteParameter[] {
                    new System.Data.SQLite.SQLiteParameter("@port", infos[0]),
                new System.Data.SQLite.SQLiteParameter ("@datetime",DateTime.Now ),
                new System.Data.SQLite.SQLiteParameter("@msg",infos[1]),
                new System.Data.SQLite.SQLiteParameter("@stack",infos[2])
                     });      
        }

        /// <summary>
        /// 对表SockServer写入SockServer的WPF工程异常信息
        /// </summary>
        /// <param name="infos">
        /// <para>数组个数必须等于3</para>
        /// <para>infos[0]:端口号</para>
        /// <para>infos[1]:错误信息</para>
        /// <para>infos[2]:堆栈信息</para>
        /// </param>
        /// <returns>返回插入的条数，-1代表传入参数错误</returns>
        public static  int  InsertSockServer(params object[] infos)
        {
            if (infos.Length != 3 || InitRight == false)
                return -1;
            string cmd = "insert into SockServer(Port,DateTime,Msg,Stack) values(@port,@datetime,@msg,@stack)";
            return   sqlhelper.ExecuteNonQuery(cmd,
                 new System.Data.SQLite.SQLiteParameter[] {
                    new System.Data.SQLite.SQLiteParameter("@port", infos[0]),
                new System.Data.SQLite.SQLiteParameter ("@datetime",DateTime.Now ),
                new System.Data.SQLite.SQLiteParameter("@msg",infos[1]),
                new System.Data.SQLite.SQLiteParameter("@stack",infos[2])
                 });
        }


        /// <summary>
        /// 对表DataLog写入数据日志
        /// </summary>
        /// <param name="infos">
        /// <para>数组个数必须等于3</para>
        /// <para>infos[0]:端口号</para>
        /// <para>infos[1]:目标GPRS</para>
        /// <para>infos[2]:源GPRS</para>
        /// <para>infos[3]:发送信息</para>
        /// </param>
        /// <returns>返回插入的条数，-1代表传入参数错误</returns>
        public static  int  InsertDataLog(params object[] infos)
        {
            if (infos.Length != 3 || InitRight == false)
                return -1;
            string cmd = "insert into DataLog(Port,DateTime,Source,Msg) values(@port,@datetime,@Source,@Msg)";
            return  sqlhelper.ExecuteNonQuery(cmd,
                 new System.Data.SQLite.SQLiteParameter[] {
                    new System.Data.SQLite.SQLiteParameter("@port", infos[0]),
                new System.Data.SQLite.SQLiteParameter ("@datetime",DateTime.Now ),
                new System.Data.SQLite.SQLiteParameter("@Source",infos[1]),
                new System.Data.SQLite.SQLiteParameter("@Msg",infos[2])
                 });
        }

        /// <summary>
        /// 记录增加的配置
        /// </summary>
        /// <param name="infos"></param>
        /// <returns></returns>
        public static int AddConfig(params object[] infos)
        {
            if (infos.Length != 4 || initRight == false)
                return -1;
            string cmd = "delete from ServerConfig where ServerPort=@ServerPort";
            sqlhelper.ExecuteNonQuery(cmd, new System.Data.SQLite.SQLiteParameter("@ServerPort", infos[1]));
             cmd = "insert into ServerConfig(ServerName,ServerPort,ServerCapacity,ServerIp) values(" +
                "@ServerName,@ServerPort,@ServerCapacity,@ServerIp) ";
            return sqlhelper.ExecuteNonQuery(cmd,
                new System.Data.SQLite.SQLiteParameter[]
                {
                    new System.Data.SQLite.SQLiteParameter("@ServerName",infos[0]),
                    new System.Data.SQLite.SQLiteParameter("@ServerPort",infos[1]),
                    new System.Data.SQLite.SQLiteParameter("@ServerCapacity",infos[2]),
                    new System.Data.SQLite.SQLiteParameter("@ServerIp",infos[3])
                });
        }

        /// <summary>
        /// 删除配置
        /// </summary>
        /// <param name="infos"></param>
        /// <returns></returns>
        public static int DeleteConfig(params object[] infos)
        {
            if (infos.Length != 1 || initRight == false)
                return -1;
            string cmd = "delete from ServerConfig where ServerPort=@ServerPort ";
            return sqlhelper.ExecuteNonQuery(cmd,
                new System.Data.SQLite.SQLiteParameter[] {
                    new System.Data.SQLite.SQLiteParameter("@ServerPort",infos[0]) });
        }


        /// <summary>
        /// 读取所有配置
        /// </summary>
        /// <returns>每条配置是DataTable里面的一条ROW</returns>
        public static DataTable ReadConfig()
        {
            if (initRight == false)
                return new DataTable();
            string cmd = "select * from ServerConfig ";
            return sqlhelper.ExecuteDataTable(cmd);
        }
    }
}
